/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.eclipse.core.resources.IResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.DirtiesProjectFiles;

/**
 * @author Xi
 * 
 */
public class LevelContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-validation-level.xml");
		// remove all markers on document
		file.deleteMarkers(ContentValidator.DEFAULT_MARKER_ID, true, IResource.DEPTH_INFINITE);
		file.deleteMarkers(ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		validator = new ContentValidator();
		validator.connect(document);
	}

	@After
	public void tearDown() throws Exception {
		validator.disconnect(document);
		super.tearDownProject();
	}

	@Test
	public void testScanAndValidateValidLevelValue() throws Exception {
		int offset = getDocumentOffset(11, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateInvalidLevelValue() throws Exception {
		int offset = getDocumentOffset(16, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateEmptyLevelValue() throws Exception {
		int offset = getDocumentOffset(21, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateMissingLevelValue() throws Exception {
		int offset = getDocumentOffset(31, 13, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewInvalidLevelValue() throws Exception {
		int offset = getDocumentOffset(11, 23, "level");
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "INFO".length(), "unknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewEmptyLevelValue() throws Exception {
		int offset = getDocumentOffset(11, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "INFO".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLevelValue() throws Exception {
		int offset = getDocumentOffset(16, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "INFO");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLevelValue() throws Exception {
		int offset = getDocumentOffset(16, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "ReallyUnknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLevelValue2() throws Exception {
		int offset = getDocumentOffset(16, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepValidLevelValue() throws Exception {
		int offset = getDocumentOffset(11, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "INFO".length(), "WARN");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepValidLevelValueCaseInsensitive() throws Exception {
		int offset = getDocumentOffset(11, 23, "level");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "INFO".length(), "info");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

}
